<template>
    <div>
        <el-card>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item>品牌商</el-breadcrumb-item>
            </el-breadcrumb>
            <div class="btnBox">
                <div>
                    <el-button size="small" @click="addHandle">添加</el-button>
                    <el-button size="small" icon="el-icon-upload2" @click="handle('up', 'top')">启用</el-button>
                    <el-button size="small" icon="el-icon-download" @click="handle('down', 'top')">禁用</el-button>
                </div>
            </div>
            <el-table :data="tableData" border style="width: 100%" height="626" @selection-change="selectChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="brandId" label="编号" width="60"></el-table-column>
                <el-table-column prop="brandName" label="品牌商名称"></el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-tag size="small" v-if="scope.row.brandStatus == '1'">已启用</el-tag>
                        <el-tag size="small" type="warning" v-else-if="scope.row.brandStatus == '0'">已禁用</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button size="small" icon="el-icon-upload2" @click="handle('up', 'table', scope.row.brandId)"
                            v-if="scope.row.brandStatus == '0'">启用</el-button>
                        <el-button size="small" icon="el-icon-download"
                            @click="handle('down', 'table', scope.row.brandId)"
                            v-if="scope.row.brandStatus == '1'">禁用</el-button>
                        <el-button size="small" icon="el-icon-edit"
                            @click="handle('edit', 'table', scope.row)">修改</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>


        <!-- 添加弹窗 -->
        <el-dialog title="添加品牌商" :visible.sync="addialogVisible" width="30%" :before-close="closeAddDialog">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="品牌商名称" prop="brandName">
                    <el-input v-model="form.brandName" placeholder="请输入品牌商名称"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="closeAddDialog">取 消</el-button>
                <el-button type="primary" @click="submitAddDialog">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 修改弹窗 -->
        <el-dialog title="修改品牌弹窗" :visible.sync="updateialogVisible" width="30%" :before-close="closeUpdateDialog">
            <el-form :model="form2" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="品牌商名称" prop="brandName">
                    <el-input v-model="form2.brandName" placeholder="请输入品牌商名称"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="closeUpdateDialog">取 消</el-button>
                <el-button type="primary" @click="submitupdateDialog">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>


import { addbrand, getbrand, disablebrand, enablebrand, updatebrand } from '../../network/brand'

export default {
    data() {
        return {
            tableData: [{
                brandName: 1
            }],
            addialogVisible: false,//添加弹窗
            updateialogVisible: false,//修改弹窗
            brandName: '',
            form: {
                brandName: '',
                brandStatus: 1,
                companyId: sessionStorage.companyId,
            },
            form2: {
                brandId: 1,
                brandName: '',
            },
            rules: {
                brandName: [
                    { required: true, message: '请输入品牌商名称', trigger: 'blur' },
                ],
            },
            selectList: [],// 选择的商品
        };
    },
    methods: {

        addHandle() {
            this.addialogVisible = true
        },

        getList() {
            getbrand({
                brandName: this.brandName
            }).then(res => {
                if (res.flag) {
                    console.log(res);
                    this.tableData = res.data;
                }
            })
        },

        handle(flag, position, obj) {
            if (position == 'top' && this.selectList.length == 0) {
                this.$message.warning('请选择要操作的数据')
                return
            }
            if (position == 'table') {
                if (flag == 'up') {
                    //启用单个
                    enablebrand({
                        Ids: obj,
                        status: 1
                    }).then(res => {
                        if (res.flag) {
                            console.log(res);
                            this.getList()
                        }
                    })
                } else if (flag == 'down') {
                    //禁用单个
                    enablebrand({
                        Ids: obj,
                        status: 0
                    }).then(res => {
                        if (res.flag) {
                            console.log(res);
                            this.getList()
                        }
                    })
                }
            }

            if (position == 'top') {
                if (flag == 'up') {
                    // 启用多个
                    let ids = '';
                    for (let i = 0; i < this.selectList.length; i++) {
                        ids = ids + this.selectList[i].brandId + ','
                    }
                    console.log(ids);
                    enablebrand({
                        Ids: ids,
                        status: 1
                    }).then(res => {
                        if (res.flag) {
                            console.log(res);
                            this.getList()
                        }
                    })
                } else {
                    // 禁用多个
                    let ids = '';
                    for (let i = 0; i < this.selectList.length; i++) {
                        ids = ids + this.selectList[i].brandId + ','
                    }
                    enablebrand({
                        Ids: ids,
                        status: 0
                    }).then(res => {
                        if (res.flag) {
                            console.log(res);
                            this.getList()
                        }
                    })
                }
            } else {
                if (flag == 'edit') {
                    // 编辑
                    this.form2 = obj;
                    console.log(this.form2);
                    this.updateialogVisible = true;
                }
            }
        },
        selectChange(val) {
            console.log(val);
            this.selectList = val
        },

        submitAddDialog() {
            this.$refs.form.validate((valid) => {
                if (!valid) return
                addbrand(this.form).then(res => {
                    if (res.flag) {
                        console.log(res);
                        this.$message.success('添加成功')
                        this.getList()
                        this.closeAddDialog()
                    }
                })
                console.log(this.form);

            });
        },
        // 关闭弹窗
        closeAddDialog() {
            this.addialogVisible = false
            this.$refs.form.resetFields();
        }
        ,
        //提交修改
        submitupdateDialog() {
            this.$refs.form.validate((valid) => {
                if (!valid) return
                updatebrand(this.form2).then(res => {
                    if (res.flag) {
                        console.log(res);
                        this.$message.success('添加成功')
                        this.getList()
                        this.closeUpdateDialog()
                    }
                })
                console.log(this.form);

            });
        },
        closeUpdateDialog() {
            this.updateialogVisible = false
            this.$refs.form.resetFields();
        }

    },
    created() {
        this.getList()
    },
    filters: {

    },
};
</script>

<style scoped></style>